import { test } from "./test";
import { createGLContext } from "./lib/creatGLContext";
import { loadShaderFromUrl } from "./lib/loadShader";
import { setupShader } from "./lib/setupShader";
import { loadObj } from "./lib/loadObj";
import { createArraysBuffer } from "./lib/createBuffer";
import { ObjObject } from "./lib/ObjObject";
import MatrixState from "./lib/MatrixState";
import { mat4 } from "gl-matrix";
import { loadTexture } from "./lib/loadTexture";
import MouseEvent  from "./lib/MouseEvent";


var me = new MouseEvent("canvas", 10, 10, function(){
    console.log(arguments);
});

var ms, obj;
// 设置变换矩阵
function setupMs(){
    ms = new MatrixState();
    ms.scale(1,1,1);
    ms.setCamera({
        /**
         * 注意坐标系，z轴指向垂直屏幕向前
         */
        eye : [0,50,100],
        center : [0,0,0],
        up : [0,1,0]
    });
    ms.setProjectFrustum({
        left    : -2.5,
        right   : 2.5,
        top     : 2.5,
        bottom  : -2.5,
        far     : 3000,
        near    : 3
    });
    ms.translate(0,-7,0);
}


// 创建上下文
var gl = createGLContext("#canvas");

// 加载着色器
var promise = new Promise(function(resolve, reject){
    loadShaderFromUrl(gl, "/dist/shader/shader", function(){
        resolve();
    });
})
.then(function(){
    // 设置着色器
    setupShader(gl);
    // 加载Obj模型
    loadObj(gl, "/dist/module/ch.obj",function(data){
        loadTexture(gl, "/dist/texture/ch.png", "ch", function(texture){
            // 设置变换矩阵
            setupMs();
            
            var vertexData = data.vertexArrayRes;   // 顶点坐标
            var textureData = data.textureArrayRes; // 纹理坐标

            obj = new ObjObject(gl, vertexData, textureData, gl.textures["ch"]);    // Obj对象
            gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
            gl.clearColor(0,0,0,1.0);
            gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 
            ms.rotate(0,1,0);
            obj.drawSelf(ms);
            /**
             * 为什么在setInterval绘制时之前的会消失
             */
            /* 
            效果不一样
            for(var i=0;i<180;i++){
                ms.rotate(0,i,0);
                obj.drawSelf(ms);
            }
            setInterval((function(){
                ms.rotate(0,1,0);
                obj.drawSelf(ms);
            }).bind(this),50);
            */
            
            var obj2 = Object.assign(new ObjObject(gl),obj);
        
            console.log(obj2)
            setInterval((function(){
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 
                ms.rotate(0,1,0);
                obj.drawSelf(ms);
            }).bind(this),50);
        })
    })
})




